/*
 * Starfield - a fake and phony representation of going through space
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;

class Starfield extends Sprite
{

	var starpos : Array<Star>;
	var bitmap : Bitmap;
	var twistfactor : Float;
	
	public function new()
	{
	
		twistfactor = 750;
	
		super();

		bitmap = new Bitmap();
		bitmap.bitmapData = new BitmapData(680, 425,false, 0x000008);
		bitmap.visible = false;
		addChild(bitmap);
		
		starpos = new Array();
		
		for (n in 0...500)
			starpos.push(new Star());
		
		for (n in starpos)
		{
			n.angle = Math.random()*3.14159*2;
			n.dist = Math.random()*425;
			n.offx = Math.random()*12 - 6;
			n.offy = Math.random()*12 - 6;
			n.coloroffset = (Math.sqrt((n.offx*n.offx)+(n.offy*n.offy))-3)*3;
			n.speed = (0.1+(n.coloroffset/10)*(n.coloroffset/10)+Math.random()/2)/3;
		}

		for (i in 0...30) // populate
		{
			for (n in starpos)
			{
				n.dist+=n.speed + (n.dist * 0.06);
				if (n.dist>(425))
					n.dist = 0;
			}
		}
		
	}

	public function up()
	{

		if (twistfactor<-750)
			twistfactor = 750;
		if (Math.abs(twistfactor)<10)
			twistfactor-=0.05;
		else if (Math.abs(twistfactor)<100)
			twistfactor-=0.2;
		else
			twistfactor-=0.4;
		
		for (n in starpos)
		{
			bitmap.bitmapData.setPixel(n.x,n.y, 0x000008);
			bitmap.bitmapData.setPixel(n.x+1,n.y, 0x000008);
			bitmap.bitmapData.setPixel(n.x,n.y+1, 0x000008);
			bitmap.bitmapData.setPixel(n.x+1,n.y+1, 0x000008);
		}
		
		for (n in starpos)
		{
			n.dist+=n.speed + (n.dist * 0.06);
			if (n.dist>(425))
				n.dist = 0;
			n.x = Std.int((680/2) + ( Math.cos(n.angle+(n.dist/twistfactor)) * n.dist ) + n.offx);
			n.y = Std.int((425/2) + ( Math.sin(n.angle+(n.dist/twistfactor)) * n.dist ) + n.offy);
			var color : Int = 0x010101 * Std.int(Math.min(Math.max(n.dist-n.coloroffset,0),375) /(375)*255);
			bitmap.bitmapData.setPixel(n.x,n.y, color);
			bitmap.bitmapData.setPixel(n.x+1,n.y, color);
			bitmap.bitmapData.setPixel(n.x,n.y+1, color);
			bitmap.bitmapData.setPixel(n.x+1,n.y+1, color);
		}
	
	}
	
}

class Star
{

	public var angle : Float;
	public var speed : Float;
	public var dist : Float;
	public var offx : Float;
	public var offy : Float;
	public var x : Int;
	public var y : Int;
	public var coloroffset : Float;
	
	public function new()
	{
	}
	
}
